/**
 * Created by Administrator on 2017/11/1.
 * Copyright 2017-2018 汉威.智慧环保事业部 Limited.
 * All rights reserved.
 */
package com.hw.one.collect.radar.Listener;

import com.hw.one.collect.radar.po.RadarSitePo;
import com.hw.one.collect.radar.service.RadarService;
import com.hw.one.collect.util.HttpClientUtil;
import com.hw.one.collect.util.OneAuthUtil;
import com.rabbitmq.client.Channel;
import org.apache.log4j.Logger;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.core.ChannelAwareMessageListener;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

public class RadarStatusReceiver implements ChannelAwareMessageListener {

    private static Logger log = Logger.getLogger(RadarStatusReceiver.class);
    private String updateSnCodeUrl = "/api/siteMonitors/";
    @Resource
    private RadarService radarService;

    @Override
    public void onMessage(Message message, Channel channel) throws Exception {
        try {
            System.out.println("consumer--:" + message.getMessageProperties().getDeliveryTag() + ":" + new String(message.getBody()));
            // 雷达状态数据暂不做处理
            String orgData = new String(message.getBody());
            RadarSitePo radarSitePo = null;
            if (null != orgData) {
                String[] rowDatas = orgData.split("\\r\\n");
                if (null != rowDatas && rowDatas.length > 0) {
                    String[] data = rowDatas[0].split(",");
                    if (null != data && data.length == 2) {
                        radarSitePo = radarService.getRadarSiteInfo(data[0]);
                        OneAuthUtil oneAuth = OneAuthUtil.getInstance();
                        // 获取验证请求头
                        Map<String, String> header = oneAuth.getAuthHeader();
//                        header.put("id", radarSitePo.getId());

                        Map<String, String> body = new HashMap<>();

                        body.put("format", "json");
                        body.put("snCode", radarSitePo.getSNCode());
                        body.put("monitorCode", radarSitePo.getMonitorCode());
                        body.put("monitorStatus", new String(message.getBody()));

                        Map<String, String> response2 = HttpClientUtil.sendPut(oneAuth.httpPre + updateSnCodeUrl+radarSitePo.getId(), header, body);
                        if (null != response2 && "200".equals(response2.get("statusCode"))) {
//                            log.info("--------------------------------------");
                            log.info(response2.get("result"));
//                            log.info("---------------------------------------");
                        } else {
                            log.info("-------------------请求失败------------");
                        }
                    }
                }

            }

            channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
        } catch (Exception e) {
            e.printStackTrace();
            channel.basicNack(message.getMessageProperties().getDeliveryTag(), false, false);
        }
    }


}
